﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using PerfilEmpreendedor.Bo.Model;
using NHibernate.Tool.hbm2ddl;



namespace PerfilEmpreendedor.Cons
{
    class Program
    {
        static void Main(string[] args)
        {
            GerarImplements(Assembly.GetAssembly(typeof(Usuario)).GetTypes());
        }
        public static void GenerateDataBase()
        {
            Fluently.Configure()
              .Database(MsSqlConfiguration.MsSql2008.ConnectionString(x => x.FromConnectionStringWithKey("DataBase")))
                .Mappings(m =>
                    m.FluentMappings.AddFromAssemblyOf<Usuario>())
                    .ExposeConfiguration(cf => new SchemaExport(cf).Create(true, true))
                .BuildSessionFactory();

            Console.Write("Bd gerado com sucesso.");
            Console.Read();


        }
        private static void GerarInterfaces(Type[] types)
        {
            StreamReader reader = new StreamReader(Directory.GetCurrentDirectory() + "\\Interfaces.txt");
            String conteudo = reader.ReadToEnd();
            reader.Close();
            reader.Dispose();
            string path = "D:\\Visual Studio 2008\\Projects\\PerfilEmpreendedor\\PerfilEmpreendedor.Dao\\Interfaces\\";
            foreach (Type t in types)
            {
                StreamWriter writer = new StreamWriter(path + "I" + t.Name + "Dao.cs");
                String conteudoSubstituido = conteudo.Replace("[className]", t.Name);
                writer.Write(conteudoSubstituido);
                writer.Close();
                writer.Dispose();

            }
        }
        private static void GerarImplements(Type[] types)
        {
            StreamReader reader = new StreamReader(Directory.GetCurrentDirectory() + "\\Implements.txt");
            String conteudo = reader.ReadToEnd();
            reader.Close();
            reader.Dispose();
            string path = "D:\\Visual Studio 2008\\Projects\\PerfilEmpreendedor\\PerfilEmpreendedor.Dao\\Implements\\";
            foreach (Type t in types)
            {
                StreamWriter writer = new StreamWriter(path + t.Name + "Dao.cs");
                String conteudoSubstituido = conteudo.Replace("[className]", t.Name);
                writer.Write(conteudoSubstituido);
                writer.Close();
                writer.Dispose();
            }
        }

        private static void GerarMaps(Type[] types)
        {
            StreamReader reader = new StreamReader(Directory.GetCurrentDirectory() + "\\Maps.txt");
            String conteudo = reader.ReadToEnd();
            reader.Close();
            reader.Dispose();
            string path = "D:\\Visual Studio 2008\\Projects\\PerfilEmpreendedor\\PerfilEmpreendedor.Bo\\Map\\";

           
            foreach (Type t in types)
            {
                StreamWriter writer = new StreamWriter(path + t.Name + "Map.cs");
                String codeMap = " Id(x => x.Id).GeneratedBy.Identity();";
                foreach (PropertyInfo p in t.GetProperties())
                {
                    codeMap += "Map(x => x." + p.Name + ");" + System.Environment.NewLine;
                  
                }
                String conteudoSubstituido = conteudo.Replace("[className]", t.Name).Replace("[codeMap]", codeMap);


                writer.Write(conteudoSubstituido);
                writer.Close();
                writer.Dispose();
            }


        }

        private static void GerarFactory(Type[] types)
        {
            StreamReader reader = new StreamReader(Directory.GetCurrentDirectory() + "\\DaoFactory.txt");
            String conteudo = reader.ReadToEnd();
            reader.Close();
            reader.Dispose();
            StreamWriter writer = new StreamWriter("D:\\Visual Studio 2008\\Projects\\PerfilEmpreendedor\\PerfilEmpreendedor.Dao\\Factory\\DaoFactory.cs");
            foreach (Type t in types)
            {
                String conteudoSubstituido = conteudo.Replace("[className]", t.Name);
                writer.WriteLine(conteudoSubstituido);
                conteudoSubstituido = string.Empty;
            }
            writer.Close();
            writer.Dispose();
        }
    }
}
